// index.js
// 获取应用实例
const app = getApp()

import {
    register,
    getEnterpriseList
} from '../../api/app.js'
import user from '../../utils/user.js'

Page({
    data: {
        tipList: [],
        enterName: '',
        authStatus: false,
        formdata: {
            company: {
                companyName: '',
                area: '',
                legalPerson: '',
                creditCode: ''
            },
            user: {
                openId: user.openid,
                userName: '',
                phone: '',
                position: '',
                userHeadUrl: ''
            }
        }
    },
    onLoad(){
        if(user.phoneNum){
            this.setData({
                'formdata.user.phone': user.phoneNum,
                authStatus: true
            })
        }
    },
    enterChange(event) {
        // if (event.detail.value.length > 2) {
            getEnterpriseList({
                name: event.detail.value
            }).then((res) => {
                this.setData({
                    tipList: res
                })
            })
        // }
        this.setData({
            'enterName': event.detail.value
        })
    },
    tapEnter(event) {
        this.setData({
            'formdata.company': {
                companyName: event.currentTarget.dataset.name,
                area: event.currentTarget.dataset.area,
                creditCode: event.currentTarget.dataset.code,
                legalPerson: event.currentTarget.dataset.legal
            },
            tipList: []
        })
        this.setData({
            'enterName': event.currentTarget.dataset.name
        })
    },
    getPhoneNumber() {
        this.setData({
            authStatus: true
        })
        this.setData({
            'formdata.user.phone': user.phoneNum
        })
    },
    submit() {
        console.log(this.data.formdata)
        if (!this.data.enterName || this.data.enterName !== this.data.formdata.company.companyName) {
            app.toast('未找到相关企业')
            return 0
        }
        if (!this.data.formdata.user.userName) {
            app.toast('请输入您的姓名')
            return 0
        }
        if (!this.data.formdata.user.position) {
            app.toast('请输入您的职位')
            return 0
        }
        this.data.formdata.user.openId = user.openid
        this.data.formdata.user.userHeadUrl = user.avatarUrl
        this.data.formdata.user.unionid = user.unionid
        this.data.formdata.user.userName = this.data.formdata.user.userName.replace(/\s*/g,'')
        this.data.formdata.user.position = this.data.formdata.user.position.replace(/\s*/g,'')
        register(this.data.formdata).then(res => {
            if (res.userId) {
                app.toast('注册成功，正在为您跳转...')
                user.reLogin().then(()=>{
                    wx.navigateTo({
                        url: '/pages/password/password?type=set&oldPass='
                    })
                })
            }
        })
    },
    userPosition(event) {
        this.setData({
            'formdata.user.position': event.detail.value
        })
    },
    userName(event) {
        this.setData({
            'formdata.user.userName': event.detail.value
        })
    }
})
